package com.mazaiting.generator.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mazaiting.generator.converter.IPropItemConverter;
import com.mazaiting.generator.domain.entity.PropItemEntity;
import com.mazaiting.generator.domain.form.PropItemForm;
import com.mazaiting.generator.domain.query.PropItemQuery;
import com.mazaiting.generator.domain.vo.PropItemVO;
import com.mazaiting.generator.mapper.IPropItemMapper;
import com.mazaiting.generator.service.IPropItemService;
import com.mazaiting.mybatisplus.domain.vo.BasePageVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 属性组
 *
 * @author mazaiting
 * @datetime 2022-11-15 00:08:52
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PropItemServiceImpl extends ServiceImpl<IPropItemMapper, PropItemEntity> implements IPropItemService {

    private final IPropItemConverter propItemConverter;

    @Override
    public BasePageVO<PropItemVO> page(PropItemQuery propItemQuery) {
        // 分页数据
        Page<PropItemEntity> page = lambdaQuery()
                .eq(PropItemEntity::getGroupId, propItemQuery.getPropGroupId())
                .page(new Page<>(propItemQuery.getPageNum(), propItemQuery.getPageSize()));
        // entity -> vo
        Page<PropItemVO> voPage = propItemConverter.entity2VO(page);

        return BasePageVO.success(voPage);
    }

    @Override
    public boolean save(PropItemForm propItemForm) {
        // form -> entity
        PropItemEntity entity = propItemConverter.form2Entity(propItemForm);
        return save(entity);
    }

    @Override
    public boolean update(PropItemForm propItemForm) {
        // 根据 ID 查询查询实体
        PropItemEntity entity = getById(propItemForm.getId());
        if (Objects.isNull(entity)) {
            log.warn("实体不存在");
        }
        // form -> entity
        propItemConverter.form2Entity(propItemForm);
        return updateById(entity);
    }

    @Override
    public boolean deleteByIds(String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).toList();
        return removeByIds(idList);
    }

    @Override
    public PropItemVO getInfoById(Long id) {
        PropItemEntity propItemEntity = getById(id);
        if (Objects.isNull(propItemEntity)) {
            log.warn("实体信息不存在");
        }
        return propItemConverter.entity2VO(propItemEntity);
    }

    @Override
    public Map<String, String> queryByGroupId(Long propGroupId) {
        return lambdaQuery()
                .eq(PropItemEntity::getGroupId, propGroupId)
                .list()
                .stream().collect(Collectors.toMap(PropItemEntity::getName, PropItemEntity::getValue));
    }

    @Override
    public List<PropItemEntity> listByGroupId(Long groupId) {
        return lambdaQuery()
                .select(PropItemEntity::getName, PropItemEntity::getValue, PropItemEntity::getRemark)
                .eq(PropItemEntity::getGroupId, groupId)
                .list();
    }

}